import store from "@/store/index";
import config from "@/config/app.config";
import {
	logoutInterface,
	} from '@/utils/olImInterface.js'
import {
		removeWatchMessage
	} from '@/utils/RongIM.js'

const getErrorMessage = (status) => {
	switch (status) {
		case 400:
			return "请求错误";
		case 401:
			return "未授权，请重新登录";
		case 403:
			return "拒绝访问";
		case 404:
			return "请求出错";
		case 408:
			return "请求超时";
		case 500:
			return "服务器错误";
		case 501:
			return "功能正在路上";
		case 502:
			return "网络错误";
		case 503:
			return "服务不可用";
		case 504:
			return "网络超时";
		case 505:
			return "HTTP版本不受支持";
		default:
			return `连接出错(${status})!`;
	}
};


var obj = {
	'zh-Hans': 'zh_CN',
	'ru': "ru_RU",
	'en': "en_Us"
}

// :FIX: 优化参数写入
const request = (
	method,
	url,
	params,
	needToken = true,
	needLoading = false,
	loadingTitle = "加载中",
	headerContentType = "application/json",
	showErrorMessage = true,
	isUpload = {
		list: ''
	},
	timeout
) => {
	var local = uni.getLocale()
	if (needLoading) {
		uni.showLoading({
			title: loadingTitle,
			mask: true
		});
	}
	const header = {
		"Content-Type": headerContentType,
		v: config.version, // 版本号
		"Content-Language": obj[local],
		 reception: 1
	};
	if (needToken) {
		header["Authorization"] = `Bearer ${store.state.token}`;
	}
	if (method === "DELETE") {
		let queryString = "?";
		if (params) {
			Object.keys(params).forEach((item, index) => {
				if (index == 0) queryString += `${item}=${params[item]}`;
				else queryString += `&${item}=${params[item]}`;
			});
			url += queryString;
		}
	}
	return new Promise((resolve, reject) => {
		isUpload.list = uni.request({
			url: `${config.server}${url}`,
			method,
			timeout: timeout || 15000, // 超时时间，单位 ms
			header,
			data: params,
			success(result) {
				if (
					result.data.code === 401 ||
					result.data.code === 4002 ||
					result.data.code === 4003
				) {
					logoutInterface()
					store.dispatch('changeToken',"")
					store.commit("CLEAR_DATA");
					removeWatchMessage()
					return uni.navigateTo({
						url: "/pages/login/Login"
					});
				}
				if (
					result.data.success ||
					result.data.code === 200 ||
					result.data.code === 4005
				) {
					uni.hideLoading();
					resolve({
						success: true,
						result: result.data
					});
				} else {
					console.log(result.data);
					uni.hideLoading();
					if (showErrorMessage) {
						uni.showToast({
							icon: "none",
							title: result.data.msg || "服务器出了点问题~",
						});
					}
					resolve({
						success: false,
						result: {}
					});
				}
			},
			fail: (error) => {
			
				uni.hideLoading();
				uni.stopPullDownRefresh(); // 收回下拉
				console.log("error", error.errMsg == 'request:fail abort');
				if(error.errMsg == 'request:fail abort') {
					reject({
						data: {
							message: "网络连接超时"
						},
					});
					return
				}
				if (!error.data) {
					uni.showToast({
						icon: "none",
						title: "网络连接超时"
					});
					reject({
						data: {
							message: "网络连接超时"
						},
					});
				}
				resolve({
					success: false,
					result: {}
				});
			},
		});
	});
};

export default request;
